home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Linux / SLAX 6.0.8 / slax-6.0.8.iso / slax / base / 006-devel.lzm / usr / include / kstaticdeleter.h < prev    next >
Encoding:
C/C++ Source or Header  |  2005-10-10  |  4.6 KB  |  140 lines

  1. /*
  2.  * This file is part of the KDE Libraries
  3.  * Copyright (C) 2000 Stephan Kulow <coolo@kde.org>
  4.  *               2001 KDE Team
  5.  *
  6.  * This library is free software; you can redistribute it and/or
  7.  * modify it under the terms of the GNU Library General Public
  8.  * License as published by the Free Software Foundation; either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * This library is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  14.  * Library General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU Library General Public License
  17.  * along with this library; see the file COPYING.LIB.  If not, write to
  18.  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  19.  * Boston, MA 02110-1301, USA.
  20.  *
  21.  */
  22.  
  23. #ifndef _KSTATIC_DELETER_H_
  24. #define _KSTATIC_DELETER_H_
  25.  
  26. #include <kglobal.h>
  27.  
  28. /**
  29.  * Static deleters are used to manage static resources. They can register
  30.  * themselves with KGlobal. KGlobal will call destructObject() when
  31.  * KGlobal::deleteStaticDeleters() is called or when it the process
  32.  * finishes.
  33.  *
  34.  * @see KStaticDeleter
  35.  * @see KGlobal::registerStaticDeleter()
  36.  * @see KGlobal::unregisterStaticDeleter()
  37.  * @see KGlobal::deleteStaticDeleters()
  38.  */
  39. class KDECORE_EXPORT KStaticDeleterBase {
  40. public:
  41.     virtual ~KStaticDeleterBase() { }
  42.     /**
  43.      * Should destruct the resources managed by this KStaticDeleterBase.
  44.      * Usually you also want to call it in your destructor.
  45.      * @see KGlobal::deleteStaticDeleters()
  46.      */
  47.     virtual void destructObject();
  48. };
  49.  
  50. /**
  51.  * Little helper class to clean up static objects that are
  52.  * held as pointer.
  53.  * When the library is unloaded, or the app terminated, all static deleters
  54.  * are destroyed, which in turn destroys those static objects properly.
  55.  * There are some rules which you should accept in the KStaticDeleter managed
  56.  * class:
  57.  * @li Don't rely on the global reference variable in the destructor of the 
  58.  * object, it will be '0' at destruction time.
  59.  * @li Don't rely on other KStaticDeleter managed objects in the destructor 
  60.  * of the object, because it may be destroyed before your destructor get called.
  61.  * This one can be tricky, because you might not know that you actually use a
  62.  * KStaticDeleter managed class. So try to keep your destructor simple.
  63.  *
  64.  * A typical use is
  65.  * \code
  66.  * static KStaticDeleter<MyClass> sd;
  67.  *
  68.  * MyClass &MyClass::self() {
  69.  *   if (!_self) { sd.setObject(_self, new MyClass()); }
  70.  *   return *_self;
  71.  * }
  72.  * \endcode
  73.  */
  74. template<class type> class KStaticDeleter : public KStaticDeleterBase {
  75. public:
  76.     KStaticDeleter() { deleteit = 0; globalReference = 0; array = false; }
  77.     /**
  78.      * Sets the object to delete and registers the object to be
  79.      * deleted to KGlobal. If the given object is 0, the former
  80.      * registration is unregistered.
  81.      * @param obj the object to delete
  82.      * @param isArray tells the destructor to delete an array instead of an object
  83.      * @deprecated See the other setObject variant.
  84.      **/
  85.     KDE_DEPRECATED type *setObject( type *obj, bool isArray = false) {
  86.         deleteit = obj;
  87.         globalReference = 0;
  88.     array = isArray;
  89.     if (obj)
  90.             KGlobal::registerStaticDeleter(this);
  91.     else
  92.         KGlobal::unregisterStaticDeleter(this);
  93.         return obj;
  94.     }
  95.     /**
  96.      * Sets the object to delete and registers the object to be
  97.      * deleted to KGlobal. If the given object is 0, the former
  98.      * registration is unregistered.
  99.      * @param globalRef the static pointer where this object is stored
  100.      * This pointer will be reset to 0 after deletion of the object.
  101.      * @param obj the object to delete
  102.      * @param isArray tells the destructor to delete an array instead of an object
  103.      **/
  104.     type *setObject( type* & globalRef, type *obj, bool isArray = false) {
  105.         globalReference = &globalRef;
  106.         deleteit = obj;
  107.     array = isArray;
  108.     if (obj)
  109.             KGlobal::registerStaticDeleter(this);
  110.     else
  111.         KGlobal::unregisterStaticDeleter(this);
  112.         globalRef = obj;
  113.     return obj;
  114.     }
  115.  
  116.     /**
  117.      * Destructs the object. This has the same effect as deleting
  118.      * the KStaticDeleter.
  119.      */
  120.     virtual void destructObject() {
  121.         if (globalReference)
  122.            *globalReference = 0;
  123.     if (array)
  124.        delete [] deleteit;
  125.     else
  126.        delete deleteit;
  127.         deleteit = 0;
  128.     }
  129.     virtual ~KStaticDeleter() {
  130.         KGlobal::unregisterStaticDeleter(this);
  131.     destructObject();
  132.     }
  133. private:
  134.     type *deleteit;
  135.     type **globalReference;
  136.     bool array;
  137. };
  138.  
  139. #endif
  140.